//int __stdcall AlphaBlit_8u_C4A(unsigned char* pSrc, int srcStep, unsigned char* pAlp, int AlpStep, unsigned char* pDst, int dstStep, IppiSize roiSize, int scaleFactor);

#include <asm.h>

.global C_SYMBOL(FillPolygon)

C_SYMBOL(FillPolygon):

width = -0x30
StartX = -0x2C
scanlinelist = -0x28
pSrc = -0x24
height = -0x20
startY = -0x1C
nDstPitch = -0x18
nSrcPitch= -0x14
pDst= -0x10

//.long 0xe7fddefe
    STMFD	SP!, {R4-R12,LR}
    SUB     SP, SP, #0x4C
Start:
    MOV	LR, R0

    LDR R11, [LR, #0x0]                 //pSrc
    STR R11, [SP,#0x4C+pSrc]
    LDR	R12, [LR, #0x4]                 //pDst
    STR R12, [SP,#0x4C+pDst]
    LDR	R9,  [LR, #0x8]                 //nSrcPitch
    STR R9,  [SP,#0x4C+nSrcPitch]
	LDR	R10, [LR, #0xC]                 //nDstPitch
	STR R10, [SP,#0x4C+nDstPitch]
	LDR	R7,  [LR, #0x10]                //startY
	STR	R7,  [SP,#0x4C+startY]
	LDR	R8,  [LR, #0x14]                //height
	STR	R8,  [SP,#0x4C+height]
//    SUB R8,  R8, #1
	LDR	R5,  [LR, #0x18]                //scanlinelist
	STR R5,  [SP,#0x4C+scanlinelist]

//    LDR     R4, = 0x00FF00FF
//    TMCRR   wR15, R4, R4

    LDR     R4, = 0x00000008
    TMCR    wCGR3, R4

    MOV     LR, #0xFF



LoopLine:

    LDR	R8, [SP,#0x4C+height]
    LDR R5, [SP,#0x4C+scanlinelist]
    MOV R0, R8, LSL #4
    ADD R5, R5, R0                      //R5 ->  scanlinelist[y]
    LDR R3, [R5]                        //StartX
    LDR R4, [R5, #8]                    //EndX
    LDR	R7, [SP,#0x4C+startY]
    SUB R5, R4, R3                      //width
    ADD R5, R5, #1
    MOV R5, R5, LSL #2
    ADD R8, R8, R7
    MLA R0, R8, R9, R11
    MLA R2, R8, R10, R12
    ADD R0, R0, R3, LSL #2
    ADD R2, R2, R3, LSL #2

	B Alpha_8u_C4ARow

Next:

    LDR	R8, [SP,#0x4C+height]
    SUBS	R8, R8,	#1
    STR	R8, [SP,#0x4C+height]

	BGE	LoopLine

End:
    ADD     SP, SP, #0x4C
	LDMFD	SP!, {R4-R12,PC}


Alpha_8u_C4ARow:

    ADD	R6, R2,	#7
    BIC	R6, R6,	#7
    SUB	R6, R6,	R2
    CMP	R6, #0
    BEQ	_Aligned8_

_Process_One_:

    WLDRW   wR4, [R0], #4
    CMP	R6, R5
    WLDRW   wR6, [R2]

    MOVGT	R6, R5
    SUB	R5, R5,	R6

_Loop_One_:

    TEXTRMUB R1, wR4, #3
    SUBS	R6, R6,	#4
    SUB     R1, LR, R1
    TBCSTH  wR5, R1
    WUNPCKELUB wR1, wR6
    WLDRWGT   wR6, [R2, #4]
    WMULUL  wR1, wR1, wR5
    WUNPCKELUB wR0, wR4
    WLDRWGT   wR4, [R0], #4
    WSRLHG    wR1, wR1, wCGR3
    WADDH     wR0, wR0, wR1

    WPACKHUS wR0, wR0, wR1

    WSTRW   wR0, [R2], #4

    BGT	_Loop_One_

    CMP	R5, #0
    BEQ     Next

_Aligned8_:
    SUBS	R5, R5,	#0x10
    ADDMI	R6, R5,	#0x10
    ADDMI	R5, R5,	#0x10
    BMI	_Process_One_
    AND	R4, R0,	#7
    CMP R4, #0
    BNE	_UnAligned8_

    WLDRD   wR0, [R0], #8
    WLDRD   wR4, [R2]

    WLDRD   wR2, [R0], #8
    WLDRD   wR5, [R2, #8]

_Loop_Aligned8_:

    SUBS	R5, R5,	#0x10
    PLD	[R0,#0x20]
    PLD	[R2,#0x30]

    TEXTRMUB R1, wR0, #3
    WUNPCKELUB wR8, wR4
    SUB     R1, LR, R1
    TBCSTH  wR7, R1
    WMULUL  wR8, wR8, wR7
    WUNPCKELUB wR6, wR0
    WSRLHG  wR8, wR8, wCGR3
    WADDH     wR6, wR6, wR8

    TEXTRMUB R1, wR0, #7
    WUNPCKEHUB wR11, wR4
    WLDRDGE wR4, [R2, #16]
    SUB     R1, LR, R1
    TBCSTH   wR10, R1
    WMULUL  wR11, wR11, wR10
    WUNPCKEHUB wR9, wR0
    WLDRDGE wR0, [R0], #8
    WSRLHG  wR11, wR11, wCGR3
    WADDH     wR9, wR9, wR11
    WPACKHUS wR6, wR6, wR9

    WSTRD   wR6, [R2], #8

    TEXTRMUB R1, wR2, #3
    WUNPCKELUB wR8, wR5
    SUB      R1, LR, R1
    TBCSTH  wR7, R1
    WMULUL  wR8, wR8, wR7
    WUNPCKELUB wR6, wR2
    WSRLHG  wR8, wR8, wCGR3
    WADDH     wR6, wR6, wR8

    TEXTRMUB R1, wR2, #7
    WUNPCKEHUB wR11, wR5
    WLDRDGE wR5, [R2, #16]
    SUB      R1, LR, R1
    TBCSTH  wR10, R1
    WMULUL  wR11, wR11, wR10
    WUNPCKEHUB wR9, wR2
    WLDRDGE wR2, [R0], #8
    WSRLHG  wR11, wR11, wCGR3
    WADDH     wR9, wR9, wR11
    WPACKHUS wR6, wR6, wR9

    WSTRD wR6, [R2], #8

    BGE	_Loop_Aligned8_
    ADDS	R5, R5,	#0x10
    BEQ     Next
    MOVGT	R6, R5
    //SUBGT   R2, R2, #8
    BGT	_Process_One_


_UnAligned8_:
    BIC	R0, R0,	#7
    TMCR    wCGR1, R4

    WLDRD   wR0, [R0], #8
    WLDRD   wR4, [R2]

    WLDRD   wR2, [R0], #8
    WLDRD   wR5, [R2, #8]

_Loop_UnAligned8_:
@.long 0xe7fddefe
    TST	R4, #7
    WLDRDNE wR12, [R0]

    SUBS	R5, R5,	#0x10
    PLD	[R0,#0x20]
    PLD	[R2,#0x30]

    WALIGNR1 wR0 ,wR0, wR2

    TEXTRMUB   R1, wR0, #3

    WUNPCKELUB wR8, wR4

    SUB     R1, LR, R1
//    .long 0xe7fddefe
    TBCSTH     wR7, R1
    WMULUL  wR8, wR8, wR7
    WUNPCKELUB wR6, wR0
    WSRLHG  wR8, wR8, wCGR3
    WADDH     wR6, wR6, wR8

    TEXTRMUB  R1, wR0, #7
    WUNPCKEHUB wR11, wR4
    WLDRDGE   wR4, [R2, #16]
    SUB       R1, LR, R1
    TBCSTH    wR10, R1
    WMULUL  wR11, wR11, wR10
    WUNPCKEHUB wR9, wR0
    WLDRDGE wR0, [R0], #8
    WSRLHG  wR11, wR11, wCGR3
    WADDH   wR9, wR9, wR11
    WPACKHUS wR6, wR6, wR9

    WSTRD   wR6, [R2], #8

    WALIGNR1 wR2 ,wR2, wR12

    TEXTRMUB R1, wR2, #3
    WUNPCKELUB wR8, wR5
    SUB      R1, LR, R1
    TBCSTH  wR7, R1
    WMULUL  wR8, wR8, wR7
    WUNPCKELUB wR6, wR2
    WSRLHG  wR8, wR8, wCGR3
    WADDH     wR6, wR6, wR8

    TEXTRMUB R1, wR2, #7
    WUNPCKEHUB wR11, wR5
    WLDRDGE wR5, [R2, #16]
    SUB      R1, LR, R1
    TBCSTH  wR10, R1
    WMULUL  wR11, wR11, wR10
    WUNPCKEHUB wR9, wR2
    WLDRDGE wR2, [R0], #8
    WSRLHG  wR11, wR11, wCGR3
    WADDH     wR9, wR9, wR11
    WPACKHUS wR6, wR6, wR9

    WSTRD wR6, [R2], #8

    BGE	_Loop_UnAligned8_

    ADD	R0, R0,	R4
    ADDS	R5, R5,	#0x10
    BEQ     Next
    MOVGT	R6, R5
    BGT	_Process_One_


